package com.commerce.weather.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.commerce.weather.Entity.DataWeather;
import com.commerce.weather.Entity.Type;
import com.commerce.weather.R;
import com.commerce.weather.utils.BitmapToAssets;
import com.commerce.weather.utils.JsonToType;

import java.util.List;

public class MyListAdapter extends BaseAdapter {
    private static final String TAG = "MyListAdapter";
    private List<DataWeather.ResultBean.FutureBean> listData;
    private Context context;
    public MyListAdapter(Context context, List<DataWeather.ResultBean.FutureBean> listData) {
        this.listData=listData;
        this.context = context;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }



    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        DataWeather.ResultBean.FutureBean futureBean = listData.get(i);
        ViewHolder viewHolder;
        if(view==null){
            Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/mjnumber.otf");
            view =LayoutInflater.from(context).inflate(R.layout.list_layout,null);
            viewHolder = new ViewHolder();
            viewHolder.weatherImage = view.findViewById(R.id.ll_icon);
            viewHolder.decsText = view.findViewById(R.id.ll_desc);
            viewHolder.decsText.setTypeface(tf);
            viewHolder.qualityText = view.findViewById(R.id.ll_quality);
            viewHolder.qualityText.setTypeface(tf);
            viewHolder.temperatureText = view.findViewById(R.id.ll_temperature);
            viewHolder.timeText = view.findViewById(R.id.ll_time);
            viewHolder.temperatureText.setTypeface(tf);
            viewHolder.timeText.setTypeface(tf);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }
        Log.d(TAG, "getView: " + futureBean.getWeather());
        Bitmap image = new BitmapToAssets().getImageFromAssetsFile(context, type(futureBean.getWid().getDay()));
        viewHolder.weatherImage.setImageBitmap(image);
        viewHolder.decsText.setText(futureBean.getWeather());
        viewHolder.temperatureText.setText(futureBean.getTemperature());
        viewHolder.timeText.setText(futureBean.getDate());
        return view;
    }

    /**
     * 天气类型
     * @param id 天气标识
     * @return 天气类型
     */
    private String type(String id){
        Type type = new JsonToType().ToType(context);
        for (Type.ResultBean resultBean:type.getResult()){
            if(resultBean.getWid().equals(id)){
                return resultBean.getWeather();
            }
        }
        return  null;
    }

    private class ViewHolder {
        private ImageView weatherImage;
        private TextView decsText;
        private TextView qualityText;
        private TextView temperatureText;
        private TextView timeText;
    }
}
